<!--
 * @Author: 马潭龙
 * @Date: 2022-01-18 15:23:56
 * @LastEditTime: 2022-01-19 19:06:35
 * @LastEditors: 马潭龙
 * @Description: 
 * @FilePath: \devlomp\icm-vue-master\src\views\hifar\regime\modules\regimeleftModal.vue
 * 可以输入预定的版权声明、个性签名、空行等
-->
<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    switchFullscreen
    @ok="handleOk"
    :okButtonProps="{ class: { 'jee-hidden': disableSubmit } }"
    @cancel="handleCancel"
    cancelText="关闭"
  >
    <j-form-container :disabled="formDisabled" ref="realForm">
      <a-form :form="form" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-item label="上级分类" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-tree-select
                v-decorator="['parentId']"
                style="width: 100%"
                placeholder="请选择父级菜单"
                :dropdownStyle="{ maxHeight: '200px', overflow: 'auto' }"
                :treeData="treeData"
                :replaceFields="replaceFields"
              />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="分类名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['title', validatorRules.title]" placeholder="请输入分类名称"></a-input>
            </a-form-item>
          </a-col>
          <a-col v-if="showFlowSubmitButton" :span="24" style="text-align: center">
            <a-button @click="submitForm">提 交</a-button>
          </a-col>
        </a-row>
      </a-form>
    </j-form-container>
  </j-modal>
</template>
<script>
import { httpAction, getAction } from '@/api/manage'
import pick from 'lodash.pick'
export default {
  props: {
    //流程表单data
    formData: {
      type: Object,
      default: () => {},
      required: false,
    },
    //表单模式：true流程表单 false普通表单
    formBpm: {
      type: Boolean,
      default: false,
      required: false,
    },
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false,
    },
    gData: {
      type: [Array, Object],
      default: () => {},
    },
  },
  watch: {
    gData: {
      immediate: true,
      handler(val) {
        this.treeData = val
      },
    },
  },
  data() {
    return {
      title: '',
      width: 800,
      visible: false,
      disableSubmit: false,
      form: this.$form.createForm(this),
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      validatorRules: {
        parentId: {
          rules: [{ required: false, message: '请输入上级分类!' }],
        },
        title: {
          rules: [{ required: true, message: '请输入分类名称!' }],
        },
      },
      treeData: {},
      replaceFields: {
        children: 'children',
        title: 'title',
        value: 'id',
        key: 'id',
      },
      value: undefined,
      url: {
        add: '/pm/hfPmProjectRegimeRecords/addRegimeRecord',
        edit: '/pm/hfPmProjectRegimeRecords/editRegimeRecord',
        treeData: '/pm/hfPmProjectRegimeRecords/projectList',
      },
    }
  },
  created() {
    this.treeData = this.gData
  },
  computed: {
    formDisabled() {
      if (this.formBpm === true) {
        if (this.formData.disabled === false) {
          return false
        }
        return true
      }
      return this.disabled
    },
    showFlowSubmitButton() {
      if (this.formBpm === true) {
        if (this.formData.disabled === false) {
          return true
        }
      }
      return false
    },
  },
  methods: {
    add() {
      this.edit({})
    },
    edit(record) {
      this.form.resetFields()
      this.model = Object.assign({}, record)
      this.visible = true
      this.$nextTick(() => {
        this.form.setFieldsValue(pick(this.model, 'parentId', 'title'))
      })
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    handleOk() {
      this.submitForm()
    },
    submitCallback() {
      this.$emit('ok')
      this.visible = false
    },
    handleCancel() {
      this.close()
    },
    submitForm() {
      const that = this
      // 触发表单验证
      this.form.validateFields((err, values) => {
        values.isFolder = '1'
        if (!err) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          let formData = Object.assign(this.model, values)
          console.log('表单提交数据', formData)
          httpAction(httpurl, formData, method)
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
                this.close()
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
            })
        }
      })
    },
  },
}
</script>